package com.sms.graduate.controller;

import com.sms.graduate.entity.SmsExamquestions;
import com.sms.graduate.entity.SmsSchoolreport;
import com.sms.graduate.entity.SmsTestpaper;
import com.sms.graduate.service.SmsExamquestionsService;
import com.sms.graduate.service.SmsSchoolreportService;
import com.sms.graduate.service.SmsTestpaperService;
import com.sms.graduate.utils.R;
import com.sms.graduate.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:jc
 * @Date:2021/11/5 10:01
 * @Description:com.sms.graduate.controller
 */
@RestController
@Api(tags = "试题和考试模块")
@RequestMapping("/exam")
@CrossOrigin("*")
public class TestPaperController {

    @Autowired
    SmsTestpaperService smsTestpaperService;

    @Autowired
    SmsSchoolreportService smsSchoolreportService;

    @Autowired
    SmsExamquestionsService smsExamquestionsService;

    @PostMapping("/objectiveGrade")
    @ApiOperation("客观题试题自动打分")
    public R objectiveGrade(@RequestParam("result") String result, @RequestParam("id") Integer id, HttpServletRequest request){
        try {
            String userCode = TokenUtils.getUserCode(request);
            List<String> objects = new ArrayList<>();
            String[] split = result.split("/");
            List<String> list = Arrays.asList(split);
            SmsTestpaper testPaper = smsTestpaperService.getById(id);
            String choiceId = testPaper.getChoiceId();
            if (StringUtils.isNotBlank(choiceId)){
                String[] split1 = choiceId.split("/");
                List<String> list1 = Arrays.asList(split1);
                objects.addAll(list1);
            }
            String judgeId = testPaper.getJudgeId();
            if (StringUtils.isNotBlank(judgeId)){
                String[] split1 = judgeId.split("/");
                List<String> list2 = Arrays.asList(split1);
                objects.addAll(list2);
            }
            String gapfillingId = testPaper.getGapfillingId();
            if (StringUtils.isNotBlank(gapfillingId)){
                String[] split1 = gapfillingId.split("/");
                List<String> list3 = Arrays.asList(split1);
                objects.addAll(list3);
            }
            int count = 0;
            for (int i = 0; i < objects.size(); i++) {
                Integer integer = Integer.valueOf(objects.get(i));
                SmsExamquestions byId = smsExamquestionsService.getById(integer);
                String s = list.get(i);
                if (byId.getResult().equalsIgnoreCase(s)){
                    count=count+byId.getGrade();
                }
            }
            SmsSchoolreport smsSchoolreport = new SmsSchoolreport();
            smsSchoolreport.setObjective(count);
            smsSchoolreport.setStudentCode(userCode);
            smsSchoolreport.setTestpaperId(id);
            smsSchoolreportService.save(smsSchoolreport);
            return R.ok().put("count",count);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

}
